from sqlalchemy import Column, Integer, String, ForeignKey, Index
from app.db.base import Base
from sqlalchemy.orm import relationship


class Item(Base):
    __tablename__ = "items"

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(100), nullable=False)  # 物品名称
    owner_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)  # 外键（级联删除）

    # 索引优化
    __table_args__ = (
        Index("idx_item_owner", "owner_id"),  # 按用户ID查询物品的索引
    )

# 关联用户 (使用字符串引用解决循环依赖)

owner = relationship("User", back_populates="items")